<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('coupon_distributions', function (Blueprint $table) {
            $table->id();
            $table->foreignId('coupon_id')->constrained()->onDelete('cascade')->comment('优惠券ID');
            $table->enum('distribution_type', ['manual', 'auto', 'activity'])
                ->default('manual')
                ->comment('发放方式：manual=手动发放, auto=自动发放, activity=活动发放');
            $table->json('target_users')->nullable()->comment('目标用户，JSON数组，null表示全部用户');
            $table->integer('quantity')->default(0)->comment('发放数量');
            $table->integer('distributed_quantity')->default(0)->comment('已发放数量');
            $table->text('notes')->nullable()->comment('备注');
            $table->foreignId('created_by')->nullable()->constrained('users')->onDelete('set null')->comment('创建人');
            $table->timestamps();
            
            $table->index('coupon_id');
            $table->index('distribution_type');
            $table->index('created_by');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('coupon_distributions');
    }
};
